﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{

    public partial class FormChiTietDonDatHang : Window
    {
        int dsRow;
        int Curr = 0;
        public int state;
        public int MaDH;
        DatHangBUS DHBUS = new DatHangBUS();
        public FormChiTietDonDatHang()
        {
            InitializeComponent();
            DataSet ds = DHBUS.GetSanPham();
            hoiThoat = false;
            textBox1.Text = "4";

        }

        private void LoadForm()
        {
            DataSet ds2 = DHBUS.SELECTTENNV();
            var items = new BindingList<KeyValuePair<string, string>>();


            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                items.Add(new KeyValuePair<string, string>(ds2.Tables[0].Rows[i][0].ToString(), ds2.Tables[0].Rows[i][0].ToString()));
            }
            CMBTen.DataSource = items;
            CMBTen.ValueMember = "Key";
            CMBTen.DisplayMember = "Value";
        }
        private void Process(DataSet ds)
        {
            //string t = CMBNV.ValueMember;
            string t = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i][0].ToString() == t)
                {
                    CMBTen.ValueMember = ds.Tables[0].Rows[i][1].ToString();
                    CMBTen.DisplayMember = ds.Tables[0].Rows[i][1].ToString();
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            DataSet ds = DHBUS.GetSanPham();

        }

        private void FormChiTietDonDatHang_Load(object sender, EventArgs e)
        {
            txtMa.Text = "1";
            this.dateTimePicker1.Value = DateTime.Now;
            LoadForm();
            if (state == 0)//Them moi
            {
                int k = DHBUS.LoadMADH();
                k++;
                labelid.Text = k.ToString();
            }
            else
            {
                if (state == 1)
                {
                    labelid.Text = MaDH.ToString();
                    DatHangBUS BUS = new DatHangBUS();
                    DataSet ds = BUS.DsSanpham(this.MaDH);
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        dataGridView1.Rows.Add(ds.Tables[0].Rows.Count);
                        dsRow = ds.Tables[0].Rows.Count;
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                                dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j];
                        }

                    }
                    DataSet ds2 = BUS.LOADDH(this.MaDH);
                    btnGhiChu.Text = ds2.Tables[0].Rows[0][4].ToString();
                    txtMa.Text = ds2.Tables[0].Rows[0][2].ToString();
                    CalcMoney();
                }
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            Curr = dataGridView1.Rows.Count - 1;
            FormChonSanPhamMini frm = new FormChonSanPhamMini();
            frm.ShowDialog();
            int masp = frm.masp;
            double dg = frm.dg;
            int cv = frm.cv;
            string tensp = frm.tensp;
            int flag = 0;
            int pos = -1;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                if (masp == int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()))
                {
                    flag = 1;
                    pos = i;
                }
            }
            if (flag == 0)
            {
                dataGridView1.Rows[Curr].Cells[0].Value = masp;
                dataGridView1.Rows[Curr].Cells[1].Value = tensp;
                dataGridView1.Rows[Curr].Cells[2].Value = cv;
                dataGridView1.Rows[Curr].Cells[3].Value = dg;

            }
            else
            {
                MessageBox.Show("Sản phẩm đã tồn tại");
                dataGridView1.CurrentCell = dataGridView1[0, pos];
            }

        }

        private void dataGridView1_CellLeave_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.Value == null || checkstring(dataGridView1.CurrentCell.Value.ToString()) == false)
            {

                MessageBox.Show("Dữ liệu không hợp lệ");

            }
            else
            {

                Curr = dataGridView1.CurrentCellAddress.Y;

                if (dataGridView1.Rows[Curr].Cells[4].Value != null && checkstring(dataGridView1.Rows[Curr].Cells[4].Value.ToString()) == true)
                {

                    if (dataGridView1.Rows[Curr].Cells[3].Value != null && checkstring(dataGridView1.Rows[Curr].Cells[3].Value.ToString()) == true)
                    {

                        string h = dataGridView1.Rows[Curr].Cells[4].Value.ToString();
                        int sl = 0;
                        double dg = 0;
                        sl = int.Parse(h);

                        h = dataGridView1.Rows[Curr].Cells[3].Value.ToString();

                        dg = double.Parse(h);

                        double tong = sl * dg;
                        dataGridView1.Rows[Curr].Cells[5].Value = tong.ToString();


                    }
                }
                if (dataGridView1.CurrentCellAddress.X == 0)
                {
                    int x = int.Parse(dataGridView1.CurrentCell.Value.ToString());
                    int k = DHBUS.FINDSP(x).Tables[0].Rows.Count;
                    int flag = 0;
                    int pos = -1;
                    for (int i = 0; i < dataGridView1.Rows.Count - 2; i++)
                    {
                        if (x == int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()))
                        {
                            flag = 1;
                            pos = i;
                        }
                    }
                    if (flag == 0)
                    {
                        DataSet ds=DHBUS.FINDSP(x);
                        dataGridView1.Rows[Curr].Cells[1].Value = ds.Tables[0].Rows[0][0].ToString();
                        dataGridView1.Rows[Curr].Cells[2].Value = ds.Tables[0].Rows[0][2].ToString();
                        dataGridView1.Rows[Curr].Cells[3].Value = ds.Tables[0].Rows[0][1].ToString();

                    }
                    else
                    {
                        MessageBox.Show("Sản phẩm đã tồn tại");
                        dataGridView1.CurrentCell = dataGridView1[0, pos];
                    }

                }
            }


        }
        private bool checkstring(string k)
        {
            double Num;
            bool isNum = double.TryParse(k, out Num);
            return isNum;
        }
        private void dataGridView1_Validated(object sender, EventArgs e)
        {
            // dataGridView1.Rows.Add(1);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }
        }
        private int checkdulieu()
        {

            int dsRow1 = dataGridView1.Rows.Count;
            for (int i = 0; i < dsRow1 - 1; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    if (dataGridView1.Rows[i].Cells[j].Value == null || (j != 1 && checkstring(dataGridView1.Rows[i].Cells[j].Value.ToString()) == false))
                    {
                        return 1;
                    }
                }
                int k = int.Parse(textBox1.Text);
                if (k > 100 || k < 0)
                {
                    return 1;
                }

            }
            if (txtMa.Text == "")
                return 1;
            return 0;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (checkdulieu() == 0&&double.Parse(label14.Text.ToString())!=0)
            {
                int flag = 1;
                int dsRow1 = dataGridView1.Rows.Count;
                if (state == 0)
                {
                    DatHangDTO dhDTO = new DatHangDTO();

                    dhDTO.GhiChu = btnGhiChu.Text;
                    dhDTO.MaDH = int.Parse(labelid.Text.ToString());
                    int ngay = dateTimePicker1.Value.Day;
                    int thang = dateTimePicker1.Value.Month;
                    int year = dateTimePicker1.Value.Year;
                    DateTime dt = new DateTime(year,thang,ngay);
                    dhDTO.NgayDat =dt;
                    dhDTO.TrangThai = false;
                    dhDTO.MaNV = int.Parse(txtMa.Text.ToString());
                    dhDTO.Diem = int.Parse(textBox1.Text.ToString());
                    flag = DHBUS.INSERTDH(dhDTO);
                    if (flag == 0)
                    {
                        MessageBox.Show("Cập nhật thất bại");
                    }
                }
                if (state == 1)//Sua
                {
                    int k = int.Parse(labelid.Text.ToString());

                    flag = DHBUS.DELETECTDH(k);
                    if (flag == 0)
                    {
                        MessageBox.Show("Cập nhật thất bại");
                    }
                    flag = 1;

                }
                if (flag == 1)
                {
                    for (int i = 0; i < dsRow1 - 1; i++)
                    {
                        CTDatHangDTO ctDTO = new CTDatHangDTO();
                        ctDTO.MaSP = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                        ctDTO.MaDH = int.Parse(labelid.Text);
                        ctDTO.NgayDat = dateTimePicker1.Value.Date;
                        ctDTO.SoLuong = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
                        ctDTO.DonGia = int.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
                        ctDTO.ThanhTien = int.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString());
                        ctDTO.ConLai = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
                        ctDTO.Diem = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                        int b = DHBUS.INSERTCTDH(ctDTO);
                        if (b == 0)
                        {
                            flag = 0;
                        }
                    }
                }
                if (flag == 1)
                {
                    MessageBox.Show("Cập nhật thành công");
                    if (state == 0)
                    {
                        int k = int.Parse(labelid.Text.ToString());
                        k++;
                        labelid.Text = k.ToString();
                    }
                }
                if (flag == 0)
                {
                    int k = int.Parse(labelid.Text.ToString());
                    DHBUS.DELETECTDH(k);
                }
            }
            else { MessageBox.Show("Tồn tại dữ liệu không họp lệ"); }


        }




        private void txtMa_TextChanged(object sender, EventArgs e)
        {
            if (checkstring(txtMa.Text.ToString()) == true)
            {
                DataSet ds = DHBUS.RETURNTENNV(int.Parse(txtMa.Text.ToString()));
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("Không tồn tại nhân viên");
                    textBox1.Text = "1";
                }
                else
                {
                    CMBTen.Text = ds.Tables[0].Rows[0][0].ToString();
                }
            }
            if (checkstring(txtMa.Text.ToString()) == false && txtMa.Text.ToString() != "")
            {
                MessageBox.Show("Mã nhân viên không hợp lệ");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            FormDSNhanVien frm = new FormDSNhanVien();
            frm.ShowDialog();
        }
        private void CalcMoney()
        {
            double Sum = 0;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                Sum += double.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString());
            }
            double CK = Sum / 100 * double.Parse(textBox1.Text.ToString());
            double SumCK = Sum - CK;
            double SumafVAT = SumCK / 100 * 110;
            label7.Text = Sum.ToString();
            label10.Text = CK.ToString();
            label11.Text = SumCK.ToString();
            label14.Text = SumafVAT.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (checkdulieu() == 0)
            {
                CalcMoney();
            }
            else
            {
                MessageBox.Show("Tồn tại dữ liệu không họp lệ");
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (checkstring(textBox1.Text) == false)
            {
                MessageBox.Show("Chỉ được nhập số");
                textBox1.Text = "";
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnXuat_Click(object sender, EventArgs e)
        {
            //XuatExcel(dataGridView1, "ChiTietDH");
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            var dt = new DataTable();
            dt.TableName = "FristTable";
            dt.Columns.Add("MaDH", typeof(int));
            dt.Columns.Add("MASP", typeof(int));
            dt.Columns.Add("TENSP", typeof(string));
            dt.Columns.Add("CV", typeof(int));
            dt.Columns.Add("DONGIA", typeof(double));
            dt.Columns.Add("SOLUONG", typeof(int));
            dt.Columns.Add("THANHTIEN", typeof(double));
            foreach (DataGridViewRow dgv in dataGridView1.Rows)
            {
                dt.Rows.Add(int.Parse(labelid.Text), dgv.Cells[0].Value, dgv.Cells[1].Value, dgv.Cells[2].Value, dgv.Cells[3].Value, dgv.Cells[4].Value, dgv.Cells[5].Value);
            }
            ds.Tables.Add(dt);
            var dt2 = new DataTable();
            dt2.TableName = "SecondTable";
            dt2.Columns.Add("MaDH", typeof(int));
            dt2.Columns.Add("NgayDat", typeof(DateTime));
            dt2.Columns.Add("NguoiDat", typeof(string));
            dt2.Columns.Add("TongTien", typeof(double));
            dt2.Columns.Add("CK", typeof(double));
            dt2.Columns.Add("TongTien2", typeof(double));
            dt2.Columns.Add("ThanhTien", typeof(double));
            dt2.Rows.Add(int.Parse(labelid.Text), dateTimePicker1.Value, CMBTen.Text, double.Parse(label7.Text), double.Parse(label10.Text), double.Parse(label11.Text), double.Parse(label14.Text));
            ds.Tables.Add(dt2);
            ds.WriteXml("data\\CTDatHang.xml");

            FormXuatChiTietDatHang frm = new FormXuatChiTietDatHang();
            frm.ShowDialog();
        }

    }
}

